package com.sduept.nwld.dataserver.controller.base.secondary;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

import org.apache.tools.ant.util.StringUtils;
import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;

import com.sduept.bigdata.weather.map.MapDataCache;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.cim.model.core.Substation;
import com.sduept.cim.model.protection.TravelingWaveEquipment;
import com.sduept.cim.model.wires.ACLineSegment;
import com.sduept.cim.model.wires.DcLinesegment;
import com.sduept.cim.query.service.ACLineSegmentQueryService;
import com.sduept.cim.query.service.DcLinesegmentQueryService;
import com.sduept.cim.query.service.TravelingWaveEquipmentQueryService;
import com.sduept.utils.PingYinUtil;

@Named
@ViewScoped
public class TravelingWaveDeviceController extends AbstractController {

	/**
	 * 行波设备维护
	 */
	private static final long serialVersionUID = -6283671555322418747L;
	@Autowired
	private MapDataCache cim;
	@Autowired
	private TravelingWaveEquipmentQueryService tweqs;
	@Autowired
	private ACLineSegmentQueryService acLineSegmentQueryService;
	@Autowired
	private DcLinesegmentQueryService dcLinesegmentQueryService;
	

	/**
	 * 列表显示数据
	 */
	private List<TravelingWaveEquipment> travelingWaveEquipments;
	/**
	 * 线路列表数据
	 */
	private List<LinesVO> lineList = new ArrayList<LinesVO>();
	public Map<String, List<String>> cimstation = new HashMap<>();
	/**
	 * 厂站列表
	 */
	private List<Substation> substations = new ArrayList<>();
	/**
	 * 搜索条件-生产厂家
	 */
	private String equipmentManufacturer = "";
	/**
	 * 搜索条件-所属变电站名
	 */
	private String subtationName = "";
	private String subtationId = "";
	
	/**
	 * 搜索条件-设备型号
	 */
	private String deviceType = "";
	
	/**
	 * 数据量总数
	 */
	private long rowCount = 0;

	@PostConstruct
	public void init() {
		travelingWaveEquipments = tweqs.listAll();
		rowCount = travelingWaveEquipments.size();
		substations = cim.getS500List();
		for (Substation r : substations) {
			String key = PingYinUtil.getPingYinHeader(r.getName());
			List<String> ss = cimstation.get(key);
			if (ss == null) {
				ss = new ArrayList<>();
				cimstation.put(key, ss);
			}
			ss.add(r.getName());
		}
	}
	
	/**
	 * 重置搜索条件
	 */
	public void reset() {
		equipmentManufacturer = "";
		subtationName = "";
		subtationId = "";
		deviceType = "";
		travelingWaveEquipments.clear();
		travelingWaveEquipments = tweqs.listAll();
		rowCount = travelingWaveEquipments.size();
	}
	
	/**
	 * 搜索方法
	 */
	public void search() {
		travelingWaveEquipments.clear();
		travelingWaveEquipments = tweqs.getWaveEquipmentBySidManufacturerDevType(subtationId,equipmentManufacturer,deviceType);
		rowCount = travelingWaveEquipments.size();
	}
	
	/**
	 * 变电站选择器
	 * @param event
	 */
	public void onStartStationSelect(SelectEvent event) {
		String stationName = (String) event.getObject();
		Substation station = getStation(stationName);
		if (null != station) {
			subtationName = station.getName();
			subtationId = station.getId();
		}
	}
	
	/**
	 * 通过变电站名称查找变电站
	 *
	 * @param stationName
	 */
	private Substation getStation(String stationName) {
		for (Entry<String, Substation> entry : cim.getStationid_map().entrySet()) {
			if (entry.getValue().getName().equals(stationName)) {
				return entry.getValue();
			}
		}
		return null;
	}
	
	/**
	 * 打开选择设备模态框
	 */
	@SuppressWarnings("deprecation")
	public void getEquipmentByStation() {
		Map<String, Object> options = new HashMap<String, Object>();
		options.put("resizable", true);
		options.put("draggable", true);
		options.put("modal", true);
		options.put("width", 600);
		options.put("height", 400);
		Map<String, List<String>> params = new HashMap<>();
		params.put("chooseStation", Arrays.asList("true"));
//		RequestContext.getCurrentInstance().openDialog("/ui/common/equipment_selector_single", options, params);
	    openDialog("/ui/common/equipment_selector_single", params, options);
	}

    /**
     *线路信息
     * @param selected
     */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void setSelected(TravelingWaveEquipment selected) {
		lineList.clear();
		List<String> lineArr = new ArrayList(StringUtils.split(selected.getMonitorLines(),','));
		List<ACLineSegment> aclines = acLineSegmentQueryService.listAllById(lineArr);
		if(aclines!= null && aclines.size()>0) {
			for (ACLineSegment acLineSegment : aclines) {
				LinesVO linesVO = new LinesVO();
				linesVO.setActualVoltage(acLineSegment.getActualVoltage());
				linesVO.setEndST(acLineSegment.getEndST());
				linesVO.setEndStName(acLineSegment.getEndStName());
				linesVO.setLineLength(acLineSegment.getLength());
				linesVO.setLineName(acLineSegment.getName());
				linesVO.setStandVoltagelevel(acLineSegment.getStandVoltagelevel());
				linesVO.setStartST(acLineSegment.getStartST());
				linesVO.setStartStName(acLineSegment.getStartStName());
				lineList.add(linesVO);
				lineArr.remove(acLineSegment.getId());
			}
		}
		List<DcLinesegment> dclines = dcLinesegmentQueryService.listAllById(lineArr);
		if(dclines!= null && dclines.size()>0) {
			for (DcLinesegment dcLineSegment : dclines) {
				LinesVO linesVO = new LinesVO();
				linesVO.setLineName(dcLineSegment.getName());
				linesVO.setActualVoltage(dcLineSegment.getActualVoltage());
				linesVO.setEndST(dcLineSegment.getEndST());
				linesVO.setEndStName(dcLineSegment.getEndStName());
				linesVO.setLineLength(dcLineSegment.getLength());
				linesVO.setStandVoltagelevel(dcLineSegment.getStandVoltagelevel());
				linesVO.setStartST(dcLineSegment.getStartST());
				linesVO.setStartStName(dcLineSegment.getStartStName());
				lineList.add(linesVO);
			}
		}
	}

	public List<TravelingWaveEquipment> getTravelingWaveEquipments() {
		return travelingWaveEquipments;
	}

	public void setTravelingWaveEquipments(List<TravelingWaveEquipment> TravelingWaveEquipments) {
		this.travelingWaveEquipments = TravelingWaveEquipments;
	}

	public String getEquipmentManufacturer() {
		return equipmentManufacturer;
	}

	public void setEquipmentManufacturer(String equipmentName) {
		this.equipmentManufacturer = equipmentName;
	}

	public String getSubtationName() {
		return subtationName;
	}

	public void setSubtationName(String subtationName) {
		this.subtationName = subtationName;
	}

	public String getDeviceType() {
		return deviceType;
	}

	public void setDeviceType(String deviceType) {
		this.deviceType = deviceType;
	}

	public long getRowCount() {
		return rowCount;
	}

	public void setRowCount(long rowCount) {
		this.rowCount = rowCount;
	}
	
	public List<LinesVO> getLineList() {
		return lineList;
	}

	public void setLineList(List<LinesVO> lineList) {
		this.lineList = lineList;
	}

	/**
	 * 页面展示线路数据使用
	 * @author lixianzhe
	 */
	public class LinesVO implements Serializable{
		/**
		 * 
		 */
		private static final long serialVersionUID = -6318685673212510544L;
		/** 线路名称 */
		private String lineName;
		/** 标准电压等级 */
		private float standVoltagelevel;

		/** 实际电压等级 */
		private float actualVoltage;
		
		/** 开始变电站Id */
		private String startST;

		/** 结束变电站Id */
		private String endST;

		/** 开始变电站名称 */
		private String startStName;

		/** 结束变电站名称 */
		private String endStName;

		/** 线路长度 */
		private Double lineLength;
		
		LinesVO(){
			
		}
		public String getLineName() {
			return lineName;
		}

		public void setLineName(String lineName) {
			this.lineName = lineName;
		}

		public float getStandVoltagelevel() {
			return standVoltagelevel;
		}

		public void setStandVoltagelevel(float standVoltagelevel) {
			this.standVoltagelevel = standVoltagelevel;
		}

		public float getActualVoltage() {
			return actualVoltage;
		}

		public void setActualVoltage(float actualVoltage) {
			this.actualVoltage = actualVoltage;
		}

		public String getStartST() {
			return startST;
		}

		public void setStartST(String startST) {
			this.startST = startST;
		}

		public String getEndST() {
			return endST;
		}

		public void setEndST(String endST) {
			this.endST = endST;
		}

		public String getStartStName() {
			return startStName;
		}

		public void setStartStName(String startStName) {
			this.startStName = startStName;
		}

		public String getEndStName() {
			return endStName;
		}

		public void setEndStName(String endStName) {
			this.endStName = endStName;
		}

		public Double getLineLength() {
			return lineLength;
		}

		public void setLineLength(Double length) {
			this.lineLength = length;
		}
		
	}


}
